<template>
  <div>
    <div v-if="mode === 'DESIGN'">
      <el-input disabled :placeholder="$t(placeholder)" show-word-limit :rows="numberLine" type="textarea" :maxlength="maxlength"/>
    </div>
    <div v-else-if="mode === 'PC' && !readonly">
      <el-input v-model="_value" clearable  :placeholder="$t(placeholder)" show-word-limit :maxlength="maxlength"
        :rows="numberLine" type="textarea">
        <template #append v-if="enableScan">
          <el-button v-if="enableScan" icon="FullScreen" @click="scanCode"></el-button>
        </template>
      </el-input>
    </div>
    <div v-else-if="mode === 'MOBILE' && !readonly">
      <field v-model="_value" :rows="numberLine" autosize type="textarea" :maxlength="maxlength"
        :placeholder="$t(placeholder)" show-word-limit>
        <template #right-icon v-if="enableScan">
          <el-icon @click="scanCode"></el-icon>
        </template>
      </field>
      <scan-code v-model="visible" @ok="scanOk"></scan-code>
    </div>
    <div v-else>
      <!-- {{ _value }} -->
      <el-input v-model="_value" clearable show-word-limit :rows="3" type="textarea" :maxlength="maxlength"
        :disabled="readonly"></el-input>
    </div>
  </div>
</template>

<script>
import { Field } from 'vant'
import ScanCode from '@/components/common/ScanCode.vue'
import componentMinxins from '../ComponentMinxins'

export default {
  mixins: [componentMinxins],
  name: 'TextareaInput',
  components: { Field, ScanCode },
  props: {
    modelValue: {
      type: String,
      default: null,
    },
    placeholder: {
      type: String,
      default: 'webAuto.enterContent',
    },
    enableScan: {
      type: Boolean,
      default: false,
    },
    numberLine: {
      type: Number,
      default: 3
    },
    maxlength: {
      type: Number,
      default: 2048,
    },
  },
  computed: {
    _value: {
      get() {
        return this.modelValue
      },
      set(val) {
        this.$emit('update:modelValue', val)
      },
    },
  },
  data() {
    return {
      visible: false,
      popupStyle: {
        height: '100%',
        width: '100%',
        background: 'black',
        opacity: '0.5',
      },
    }
  },
  methods: {
    scanCode() {
      this.visible = true
    },
    scanOk(code) {
      this._value += code + '\n'
      showSuccessToast(this.$t('webAuto.ScanCodeSuccessfully'))
    },
  },
  emits: ['update:modelValue'],
}
</script>

<style scoped></style>
